sfxChat = { }
sfxChat.players = { }

if minetest.setting_getbool("lunomods_soundchat")~= false then
	minetest.setting_set("lunomods_soundchat", "true")
end

			
minetest.register_on_chat_message(function(name,msg)
	if msg~=nil and msg:len()>=2 then
		if minetest.setting_getbool("lunomods_soundchat")~= false then
			for i,player in ipairs(minetest.get_connected_players()) do
				if player~=nil and player:is_player() and player:get_player_name()~=nil and player:get_player_name() ~= name then --Toca para todos ,exceto para quem enviou a mensagem.
					local targetname = player:get_player_name()
					if sfxChat.players[targetname]~=nil then 
						minetest.sound_stop(sfxChat.players[targetname])
					end
					--print("minetest.register_on_chat_message() >>>> targetname='"..targetname.."' | msg='"..msg.."'")
					if 
						string~=nil and string.len~=nil and string.find~=nil and string.lower~=nil and 
						msg~=nil and type(msg)=="string" and 
						targetname~=nil and type(targetname)=="string" 
						and (
							string.find(string.lower(msg), string.lower(targetname)) 
							or ( 
								string.len(msg)>=3 and 
								string.find(
									string.lower(targetname), 
									string.lower(msg)
								) 
							)
						)
					then
						--#################### CHAMAR ATENÇÃO #########################################################
						sfxChat.players[targetname] = minetest.sound_play("sfx_chat3", {
							object = player, --Se retirar esta linha tocará para todos. (Provavelmente ¬¬)
							gain = 1.0, -- 1.0 = Volume total
							max_hear_distance = 1,
							loop = false,
						})
					elseif 
						minetest.get_modpath("lunoturns") == nil
						or lunoturns==nil 
						or (
							minetest.setting_getbool("lunomods_bgmusic") 
							and lunoturns.players[targetname]~=nil 
							and lunoturns.players[targetname].mute~=nil 
							and lunoturns.players[targetname].mute==false
						) 
					then
						--#################### CONVERSA COMUM #########################################################
						sfxChat.players[targetname] = minetest.sound_play("sfx_chat", {
							object = player, --Se retirar esta linha tocará para todos. (Provavelmente ¬¬)
							gain = 1.0, -- 1.0 = Volume total
							max_hear_distance = 1,
							loop = false,
						})
					end
					
				end
			end
		end
	end
	--return true
end)
